package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.WxCommentListBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCommentCountVo;
import com.cskaoyan.bean.vo.WxCommentPostVo;
import com.cskaoyan.service.WxCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 评论
 * @Author: JianhuaZeng
 * @Date: 2023/1/2 10:50
 */
@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    WxCommentService commentService;

    /**
     * 查询所有评论
     * @param commentListBo
     * @return
     */
    @GetMapping("list")
    public BaseRespVo list(WxCommentListBo commentListBo){
        CommonData commonData = commentService.list(commentListBo);
        return BaseRespVo.ok(commonData,"成功");
    }

    /**
     * 查询评论的数量
     * @param valueId
     * @param type
     * @return
     */
    @GetMapping("count")
    public BaseRespVo count(Integer valueId,Byte type){
        WxCommentCountVo commentCountVo = commentService.count(valueId, type);
        return BaseRespVo.ok(commentCountVo,"成功");
    }

    /**
     * 添加评论
     * @param marketComment
     * @return
     */
    @PostMapping("post")
    public BaseRespVo post(@RequestBody MarketComment marketComment){
        //获取当前用户的id
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();

        // 构造查询对象
        Date date = new Date();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketComment.setDeleted(false);
        marketComment.setUserId(userId);
        Integer id = commentService.post(marketComment);


        // 构造返回对象
        WxCommentPostVo commentPostVo = new WxCommentPostVo();
        commentPostVo.setId(id);
        commentPostVo.setValueId(marketComment.getId());
        commentPostVo.setType(marketComment.getType());
        commentPostVo.setContent(marketComment.getContent());
        commentPostVo.setUserId(userId);
        commentPostVo.setHasPicture(marketComment.getHasPicture());
        commentPostVo.setPicUrls(marketComment.getPicUrls());
        commentPostVo.setStar(marketComment.getStar());
        commentPostVo.setAddTime(date);
        commentPostVo.setUpdateTime(date);
        return BaseRespVo.ok(commentPostVo,"成功");
    }

}
